package com.transaction.demo.accounting.biz.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.accounting.biz.AccountingVoucherBiz;
import com.transaction.demo.accounting.dao.AccountingVoucherDao;
import com.transaction.demo.accounting.entity.AccountingVoucherBean;
import com.transaction.demo.accounting.vo.req.AccountingVoucherAddReqVo;
import com.transaction.demo.accounting.vo.req.AccountingVoucherListReqVo;
import com.transaction.demo.accounting.vo.req.AccountingVoucherReqVo;
import com.transaction.demo.accounting.vo.req.AccountingVoucherUpdateReqVo;
import com.transaction.demo.accounting.vo.resp.AccountingVoucherRespVo;
import com.transaction.demo.common.util.ResultPage;

/**
 * 描述：会计原始凭证表
 * 作者：马庆智
 * 日期：2023/04/27 17:54:01
 */
@Slf4j
@Service
public class AccountingVoucherBizImpl implements AccountingVoucherBiz {

	@Autowired
	private AccountingVoucherDao accountingVoucherDao;

	/**
	 * 查询会计原始凭证表
	 *
	 * @param accountingVoucher 请求参数
	 * @return AccountingVoucherResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:54:01
	 */
	@Override
	public AccountingVoucherRespVo queryAccountingVoucher(AccountingVoucherReqVo accountingVoucher) {

		AccountingVoucherBean query = accountingVoucherDao.queryAccountingVoucher(
				AccountingVoucherBean.builder().build());

		return AccountingVoucherRespVo.builder().build();
	}

	/**
	 * 查询会计原始凭证表列表
	 *
	 * @param accountingVoucher 请求参数
	 * @return AccountingVoucherResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:54:01
	 */
	@Override
	public ResultPage<AccountingVoucherRespVo> queryAccountingVoucherList(
			AccountingVoucherListReqVo accountingVoucher) {

		PageHelper.startPage(accountingVoucher.getCurrent(), accountingVoucher.getPageSize());
		List<AccountingVoucherBean> query = accountingVoucherDao.queryAccountingVoucherList(
				AccountingVoucherBean.builder().bankOrderNo(accountingVoucher.getBankOrderNo()).build());
		PageInfo<AccountingVoucherBean> pageInfo = new PageInfo<>(query);

		List<AccountingVoucherRespVo> list = pageInfo.getList().stream().map(x -> {
			AccountingVoucherRespVo resp = new AccountingVoucherRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加会计原始凭证表
	 *
	 * @param accountingVoucher 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:54:01
	 */
	@Override
	public int saveAccountingVoucher(AccountingVoucherAddReqVo accountingVoucher) {

		AccountingVoucherBean query = accountingVoucherDao.queryAccountingVoucher(
				AccountingVoucherBean.builder().bankOrderNo(accountingVoucher.getBankOrderNo()).build());

		if (query != null) {
			log.info("凭证：{}已存在", accountingVoucher.getBankOrderNo());
			return 1;
		}

		AccountingVoucherBean bean = new AccountingVoucherBean();
		BeanUtils.copyProperties(accountingVoucher, bean);
		return accountingVoucherDao.saveAccountingVoucher(bean);
	}

	/**
	 * 删除会计原始凭证表
	 *
	 * @param ids 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:54:01
	 */
	@Override
	public int deleteAccountingVoucher(String ids) {
		return accountingVoucherDao.deleteAccountingVoucher(
				Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * 更新会计原始凭证表
	 *
	 * @param accountingVoucher 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:54:01
	 */
	@Override
	public int updateAccountingVoucher(AccountingVoucherUpdateReqVo accountingVoucher) {

		return accountingVoucherDao.updateAccountingVoucher(AccountingVoucherBean.builder().build());
	}

}